package controller

import (
	"encoding/json"
	"fmt"
	"mbti/model"

	"github.com/gin-gonic/gin"
)

//获取测试结果  算法也在这里
func MbtiTest(ctx *gin.Context) {

	data := make(map[string]string) //注该结构接受的内容
	ctx.BindJSON(&data)

	studentID := data["studentID"]
	delete(data, "studentID")
	jsonData, _ := json.Marshal(data)

	score := make(map[string]int)
	score["e"] = 0
	score["s"] = 0
	score["t"] = 0
	score["j"] = 0
	score["i"] = 0
	score["n"] = 0
	score["f"] = 0
	score["p"] = 0

	//核心算法 仅此一行
	for k, v := range data {
		score[Rule[v+k]] += 1
	}

	var finalResult string

	if (score["i"] * 9) >= (score["e"] * 12) {
		finalResult += "I"
	} else {
		finalResult += "E"
	}
	if (score["n"] * 18) >= (score["s"] * 9) {
		finalResult += "N"
	} else {
		finalResult += "S"
	}
	if (score["f"] * 9) >= (score["t"] * 14) {
		finalResult += "F"
	} else {
		finalResult += "T"
	}
	if (score["p"] * 8) >= (score["j"] * 14) {
		finalResult += "P"
	} else {
		finalResult += "J"
	}
	fmt.Println(data)
	fmt.Println("finalResult", finalResult)

	isOk := model.InsertAnswer(studentID, string(jsonData), finalResult)
	if isOk == false {
		ctx.JSON(422, gin.H{
			"code": 422,
			"msg":  "添加失败",
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  finalResult,
	})
}

var Rule map[string]string

//go语言特性  初始化函数  仅执行一次
func init() {
	Rule = make(map[string]string)
	Rule["A1"] = "j"
	Rule["B1"] = "p"
	Rule["A2"] = "p"
	Rule["B2"] = "j"
	Rule["A3"] = "s"
	Rule["B3"] = "n"
	Rule["A4"] = "e"
	Rule["B4"] = "i"
	Rule["A5"] = "n"
	Rule["B5"] = "s"
	Rule["A6"] = "f"
	Rule["B6"] = "t"
	Rule["A7"] = "p"
	Rule["B7"] = "j"
	Rule["A8"] = "e"
	Rule["B8"] = "i"
	Rule["A9"] = "j"
	Rule["B9"] = "p"
	Rule["A10"] = "j"
	Rule["B10"] = "p"
	Rule["A11"] = "p"
	Rule["B11"] = "j"
	Rule["A12"] = "i"
	Rule["B12"] = "e"
	Rule["A13"] = "s"
	Rule["B13"] = "n"
	Rule["A14"] = "e"
	Rule["B14"] = "i"
	Rule["A15"] = "n"
	Rule["B15"] = "s"
	Rule["A16"] = "j"
	Rule["B16"] = "f"
	Rule["A17"] = "p"
	Rule["B17"] = "j"
	Rule["A18"] = "i"
	Rule["B18"] = "e"
	Rule["A19"] = "e"
	Rule["B19"] = "i"
	Rule["A20"] = "j"
	Rule["B20"] = "p"
	Rule["A21"] = "p"
	Rule["B21"] = "j"
	Rule["A22"] = "i"
	Rule["B22"] = "e"
	Rule["A23"] = "e"
	Rule["B23"] = "i"
	Rule["A24"] = "n"
	Rule["B24"] = "s"
	Rule["A25"] = "p"
	Rule["B25"] = "j"
	Rule["A26"] = "i"
	Rule["B26"] = "e"
	Rule["A27"] = "i"
	Rule["B27"] = "e"
	Rule["A28"] = "j"
	Rule["B28"] = "p"
	Rule["A29"] = "j"
	Rule["B29"] = "p"
	Rule["A30"] = "n"
	Rule["B30"] = "s"
	Rule["A31"] = "t"
	Rule["B31"] = "f"
	Rule["A32"] = "s"
	Rule["B32"] = "n"
	Rule["A33"] = "p"
	Rule["B33"] = "j"
	Rule["A34"] = "e"
	Rule["B34"] = "i"
	Rule["A35"] = "i"
	Rule["B35"] = "e"
	Rule["A36"] = "j"
	Rule["B36"] = "p"
	Rule["A37"] = "n"
	Rule["B37"] = "s"
	Rule["A38"] = "f"
	Rule["B38"] = "t"
	Rule["A39"] = "t"
	Rule["B39"] = "f"
	Rule["A40"] = "s"
	Rule["B40"] = "n"
	Rule["A41"] = "p"
	Rule["B41"] = "j"
	Rule["A42"] = "i"
	Rule["B42"] = "e"
	Rule["A43"] = "j"
	Rule["B43"] = "p"
	Rule["A44"] = "n"
	Rule["B44"] = "s"
	Rule["A45"] = "f"
	Rule["B45"] = "t"
	Rule["A46"] = "t"
	Rule["B46"] = "f"
	Rule["A47"] = "s"
	Rule["B47"] = "n"
	Rule["A48"] = "i"
	Rule["B48"] = "e"
	Rule["A49"] = "j"
	Rule["B49"] = "p"
	Rule["A50"] = "n"
	Rule["B50"] = "s"
	Rule["A51"] = "f"
	Rule["B51"] = "t"
	Rule["A52"] = "t"
	Rule["B52"] = "f"
	Rule["A53"] = "s"
	Rule["B53"] = "n"
	Rule["A54"] = "i"
	Rule["B54"] = "e"
	Rule["A55"] = "n"
	Rule["B55"] = "s"
	Rule["A56"] = "f"
	Rule["B56"] = "t"
	Rule["A57"] = "t"
	Rule["B57"] = "f"
	Rule["A58"] = "s"
	Rule["B58"] = "n"
	Rule["A59"] = "j"
	Rule["B59"] = "p"
	Rule["A60"] = "i"
	Rule["B60"] = "e"
	Rule["A61"] = "s"
	Rule["B61"] = "n"
	Rule["A62"] = "e"
	Rule["B62"] = "i"
	Rule["A63"] = "n"
	Rule["B63"] = "s"
	Rule["A64"] = "f"
	Rule["B64"] = "t"
	Rule["A65"] = "p"
	Rule["B65"] = "j"
	Rule["A66"] = "i"
	Rule["B66"] = "e"
	Rule["A67"] = "j"
	Rule["B67"] = "p"
	Rule["A68"] = "t"
	Rule["B68"] = "f"
	Rule["A69"] = "j"
	Rule["B69"] = "p"
	Rule["A70"] = "j"
	Rule["B70"] = "p"
	Rule["A71"] = "p"
	Rule["B71"] = "j"
	Rule["A72"] = "i"
	Rule["B72"] = "e"
	Rule["A73"] = "s"
	Rule["B73"] = "n"
	Rule["A74"] = "n"
	Rule["B74"] = "s"
	Rule["A75"] = "t"
	Rule["B75"] = "f"
	Rule["A76"] = "p"
	Rule["B76"] = "j"
	Rule["A77"] = "e"
	Rule["B77"] = "i"
	Rule["A78"] = "t"
	Rule["B78"] = "f"
	Rule["A79"] = "n"
	Rule["B79"] = "s"
	Rule["A80"] = "f"
	Rule["B80"] = "t"
	Rule["A81"] = "t"
	Rule["B81"] = "f"
	Rule["A82"] = "s"
	Rule["B82"] = "n"
	Rule["A83"] = "n"
	Rule["B83"] = "s"
	Rule["A84"] = "f"
	Rule["B84"] = "t"
	Rule["A85"] = "t"
	Rule["B85"] = "f"
	Rule["A86"] = "s"
	Rule["B86"] = "n"
	Rule["A87"] = "n"
	Rule["B87"] = "s"
	Rule["A88"] = "f"
	Rule["B88"] = "t"
	Rule["A89"] = "t"
	Rule["B89"] = "f"
	Rule["A90"] = "s"
	Rule["B90"] = "n"
	Rule["A91"] = "f"
	Rule["B91"] = "t"
	Rule["A92"] = "t"
	Rule["B92"] = "f"
	Rule["A93"] = "s"
	Rule["B93"] = "n"
}
